package com.think.teaching.controller;


import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.NewsType;
import com.think.teaching.service.impl.NewsTypeServiceImpl;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 资源类型 前端控制器
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Controller
@RequestMapping("/v1")
@ResponseBody
@Api(tags = "文章类型管理接口")
public class NewsTypeController extends BaseController{
	
	private final NewsTypeServiceImpl newsTypeService;

	public NewsTypeController(NewsTypeServiceImpl newsTypeService) {
		this.newsTypeService = newsTypeService;
	}

	@ApiOperation("添加新的文章类型")
	@PostMapping("admin/newsType")
	public Result AddNewNewsType(@RequestBody()NewsType data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			NewsType newsType = newsTypeService.InsetNewsType(data);
			if( newsType!= null) {
				return Result.success(newsType);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("更新文章类型")
	@RequestMapping(value = "admin/newsType",method = RequestMethod.PUT)
	public Result UpdateNewsType(@RequestBody()NewsType data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			NewsType newsType = newsTypeService.UserSelectNewsType(data.getId(), false);
			if (newsType == null) {
				return Result.failure(ResultCode.ARTICLE_TYPE_NOT_DEFINE);
			}
			newsType = newsTypeService.SelectNewsTypeWithName(data.getName(), false);
			if (newsType != null && !newsType.getId().equals(data.getId())) {
				return Result.failure(ResultCode.ARTICLE_TYPE_ALREADY_EXIST);
			}
			newsType = newsTypeService.UpdateNewsType(data);
			if( newsType!= null) {
				return Result.success(newsType);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("删除文章类型")
	@RequestMapping(value = "admin/newsType/{id}",method = RequestMethod.DELETE)
	public Result DeleteNewsType(@PathVariable("id")Integer id) {
		NewsType newsType = newsTypeService.UserSelectNewsType(id, false);
		if( newsType== null) {
			return Result.failure(ResultCode.ARTICLE_TYPE_NOT_DEFINE);
		}
		else {
			if (newsTypeService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
		}
	}
	
	@ApiOperation("获取文章类型列表")
	@RequestMapping(value = "admin/newsTypes/{page}/{count}",method = RequestMethod.GET)
	public Result DeleteNewsType(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(newsTypeService.ListNewsType(page, count, false));

	}
	
	@ApiOperation("获取文章类型详情")
	@RequestMapping(value = "/newsType/{id}",method = RequestMethod.GET)
	public Result GetNewsType(@PathVariable("id")Integer id) {
		NewsType newsType = newsTypeService.UserSelectNewsType(id, true);
		if( newsType== null) {
			return Result.failure(ResultCode.ARTICLE_TYPE_NOT_DEFINE);
		}
		else {
			
			return Result.success(newsType);
		}
	}
	
	@ApiOperation("管理员搜索文章类型")
	@RequestMapping(value = "admin/newsTypes/search", method = RequestMethod.GET)
	public Result AdminSearchNewsType(@RequestParam("key") String key) {
		return Result.success(newsTypeService.ListNewsTypeSearch(key, false));
	}
	
	@ApiOperation("管理员修改文章类型状态")
	@RequestMapping(value = "admin/newsType/{id}/{state}", method = RequestMethod.PUT)
	public Result UpdateNewsTypeState(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
		NewsType newsType = newsTypeService.UserSelectNewsType(id, false);
		if (newsType == null) {
			return Result.failure(ResultCode.ARTICLE_TYPE_NOT_DEFINE);
		} else {
			try {
				newsType = newsTypeService.SetNewsTypeState(id, state);
				return Result.success(newsType);
			} catch (Exception e) {
				// TODO: handle exception
				return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
			}
		}
	}
	
}

